//
//  TabBarController.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class BuyAutoTabBarController: UITabBarController {
    
    struct TabItem {
        var viewController: UINavigationController
        var title: String
        var image: UIImage?
        var index: Int
    }
    
    let tabBarItems: [TabItem] = [
        TabItem(viewController: TransportRootViewController(rootViewController: TransportViewController()),
                title: "Транспорт",
                image: UIImage(named: "transport"),
                index: 0),
        TabItem(viewController: MoreRootViewController(rootViewController: AdsViewController()),
                title: "Объявления",
                image: UIImage(named: "ads"),
        index: 1),
        TabItem(viewController: MoreRootViewController(rootViewController: HomeViewConroller()),
                title: "Настройки",
                image: UIImage(named: "setting"),
                index: 2),
        TabItem(viewController: UINavigationController(),
                title: "Выйти",
                image: UIImage(named: "out"),
                index: 3)
    ]
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        viewControllers = tabBarItems.map {
            let nvc = $0.viewController
            nvc.tabBarItem = UITabBarItem(title: $0.title, image: $0.image, tag: $0.index)
            nvc.tabBarItem.tag = $0.index
            return nvc
        }
        self.tabBar.barTintColor  = .white
        self.tabBar.isTranslucent = false
    }
}

extension BuyAutoTabBarController: UITabBarControllerDelegate {


    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController.tabBarItem.tag == 3 {
            print("Sign out")
            return false
        } else {
            return true
        }
    }
}
